package org.jeecg.modules.api.controller.daping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.api.controller.daping.mapper.DaPingMapper;
import org.jeecg.modules.api.controller.daping.vo.HyDaPingVO;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hyarea.entity.HyArea;
import org.jeecg.modules.wlhy.hyarea.service.IHyAreaService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/api/wlhy/daping")
@Api(tags = "大屏")
@Slf4j
public class BaiDuyewuDaPingController {

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private DictUtil dictUtil;

    @Autowired
    private DaPingMapper daPingMapper;

    @Autowired
    private IHyAreaService hyAreaService;

    @Autowired
    private IHyOrderService orderService;

    @Autowired
    private IHyDriverService driverService;

    @Autowired
    private IHyVehicleService vehicleService;

    @Autowired
    private IHyCustomerService customerService;

    //实时监控
    @PostMapping(value = "/getssjkMap")
    public String getssjkMap() {
        JSONObject res = new JSONObject();
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();

        List<HyDaPingVO> getshippings = daPingMapper.getshippings();
        for (HyDaPingVO getshipping : getshippings) {
            JSONObject mapDataObj2 = new JSONObject();
            HyArea sendAreaCode = getAreaNames(getshipping.getSend() + "000000000");
            HyArea receiveAreaCode = getAreaNames(getshipping.getReceive() + "000000000");

            if (sendAreaCode != null && receiveAreaCode != null) {
                List<String> sendNames = Arrays.asList(sendAreaCode.getMergerName().split(","));
                List<String> receiveNames = Arrays.asList(receiveAreaCode.getMergerName().split(","));
                String[] locations = {sendNames.get(0), receiveNames.get(0)};
                mapDataObj2.put("locations", locations);
                mapDataObj2.put("value", getshipping.getYundanshu() == null ? "0" : getshipping.getYundanshu());
                mapDataObj2.put("sizeValue", getshipping.getJineshu() == null ? "0" : getshipping.getJineshu());
                mapDataObj2.put("name", sendNames.get(0) + "-" + receiveNames.get(0));
                String[] tooltipValues = {getshipping.getDunshu() == null ? "0" : getshipping.getDunshu()};
                mapDataObj2.put("tooltipValues", tooltipValues);
                mapData.add(mapDataObj2);
            }

        }

        data.put("valueName", "运单数");
        data.put("sizeValueName", "成交金额");
        String[] tooltipNames = {"发运吨数"};
        data.put("tooltipNames", tooltipNames);

        data.put("mapData", mapData);

        res.put("status", 0);
        res.put("msg", "");
        res.put("data", data);
        return res.toJSONString();
    }

    //实时监控2
    @PostMapping(value = "/getssjkMap2")
    public String getssjkMap2(String type) {

        //金猫要求数据大屏值能指定修改

        List<JSONObject> list = new ArrayList<>();
        if (HyCfgUtil.getCfg("daping2")!=null&&HyCfgUtil.isUsed("daping2")){

            JSONObject res = new JSONObject();
            res.put("type", "yds");//运单数
            int count = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","yds"));
            res.put("data", count);
            list.add(res);


            Double d = Double.valueOf(HyCfgUtil.getCfgItem2("daping2","ds"));
            d = d / 10000;
            JSONObject res2 = new JSONObject();
            res2.put("type", "ds");//运输总量
            res2.put("data", String.format("%.2f", d));
            list.add(res2);



            Double d2 = Double.valueOf(HyCfgUtil.getCfgItem2("daping2","je"));
            d2 = d2 / 10000;
            JSONObject res3 = new JSONObject();
            res3.put("type", "je");//成交金额
            res3.put("data", String.format("%.2f", d2));
            list.add(res3);

            int count2 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","che"));
            JSONObject res4 = new JSONObject();
            res4.put("type", "che");//车辆数
            res4.put("data", count2);
            list.add(res4);

            int count3 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","siji"));
            JSONObject res5 = new JSONObject();
            res5.put("type", "siji");//司机数
            res5.put("data", count3);
            list.add(res5);

            int count4 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","huozhu"));
            JSONObject res6 = new JSONObject();
            res6.put("type", "huozhu");//货主(企业数)
            res6.put("data", count4);
            list.add(res6);

            int count5 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","daijiedan"));
            JSONObject res7 = new JSONObject();
            res7.put("type", "daijiedan");//待接单
            res7.put("data", count5);
            list.add(res7);


            int count6 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","yunshuzhong"));
            JSONObject res8 = new JSONObject();
            res8.put("type", "yunshuzhong");//在输运
            res8.put("data", count6);
            list.add(res8);


            int count7 = Integer.parseInt(HyCfgUtil.getCfgItem2("daping2","yichang"));
            JSONObject res9 = new JSONObject();
            res9.put("type", "yichang");//异常单
            res9.put("data", count7);
            list.add(res9);
        }else {

            JSONObject res = new JSONObject();
            res.put("type", "yds");//运单数
            int count = hyShippingNoteService.count();
            res.put("data", count);
            list.add(res);

            HyDaPingVO getyunshuzongliang = daPingMapper.getyunshuzongliang();
            Double d = Double.valueOf(getyunshuzongliang.getValue());
            d = d / 10000;
            JSONObject res2 = new JSONObject();
            res2.put("type", "ds");//运输总量
            res2.put("data", String.format("%.2f", d));
            list.add(res2);


            HyDaPingVO getjine = daPingMapper.getjine();
            if (getjine == null) {
                getjine = new HyDaPingVO();
                getjine.setValue("0");
            }
            Double d2 = Double.valueOf(getjine.getValue());
            d2 = d2 / 10000;
            JSONObject res3 = new JSONObject();
            res3.put("type", "je");//成交金额
            res3.put("data", String.format("%.2f", d2));
            list.add(res3);

            int count2 = vehicleService.count();
            JSONObject res4 = new JSONObject();
            res4.put("type", "che");//车辆数
            res4.put("data", count2);
            list.add(res4);

            int count3 = driverService.count();
            JSONObject res5 = new JSONObject();
            res5.put("type", "siji");//司机数
            res5.put("data", count3);
            list.add(res5);

            int count4 = customerService.count();
            JSONObject res6 = new JSONObject();
            res6.put("type", "huozhu");//货主(企业数)
            res6.put("data", count4);
            list.add(res6);

            int count5 = hyShippingNoteService.count(new QueryWrapper<HyShippingNote>()
                    .eq("shipping_status", "1"));//改为已接单，参数名称没改
            JSONObject res7 = new JSONObject();
            res7.put("type", "daijiedan");//已接单
            res7.put("data", count5);
            list.add(res7);


            int count6 = hyShippingNoteService.count(new QueryWrapper<HyShippingNote>()
                    .eq("shipping_status", "3"));
            JSONObject res8 = new JSONObject();
            res8.put("type", "yunshuzhong");//在输运
            res8.put("data", count6);
            list.add(res8);


            int count7 = hyShippingNoteService.count(new QueryWrapper<HyShippingNote>()
                    .isNotNull("examine_remark").ne("shipping_status", "8"));
            JSONObject res9 = new JSONObject();
            res9.put("type", "yichang");//异常单
            res9.put("data", count7);
            list.add(res9);
        }


        return list.toString();

    }

    //发货排名
    @PostMapping(value = "/getPaiming")
    public String getPaiming() {
        List<HyDaPingVO> getkehupaiming = daPingMapper.getkehupaimingTop5();

        List<String> categories = new ArrayList<String>();
        List<String> data2 = new ArrayList<String>();
        JSONArray series = new JSONArray();
        JSONObject serie = new JSONObject();

        JSONObject res = new JSONObject();
        JSONObject data = new JSONObject();

        for (int i = 0; i < getkehupaiming.size(); i++) {
            categories.add(getkehupaiming.get(i).getName());
            data2.add(getkehupaiming.get(i).getValue());
        }
        serie.put("data", data2);
        serie.put("name", "");
        series.add(serie);

        data.put("categories", categories);
        data.put("series", series);

        res.put("status", 0);
        res.put("msg", "");
        res.put("data", data);
        return res.toJSONString();
    }


    //发货订单
    @PostMapping(value = "/getFaHuoDingDan")
    public String getFaHuoDingDan() {

        List<String> resultList = new ArrayList<String>();
        Calendar c = null;
        //近7个日
        for (int i = 0; i < 7; i++) {
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -i);
            resultList.add(c.get(Calendar.YEAR)
                    + "-" + (c.get(Calendar.MONTH) + 1 < 10 ? "0" +
                    (c.get(Calendar.MONTH) + 1) : (c.get(Calendar.MONTH) + 1)) + "-" +
                    (c.get(Calendar.DAY_OF_MONTH) < 10 ? "0" +
                            (c.get(Calendar.DAY_OF_MONTH)) : (c.get(Calendar.DAY_OF_MONTH)))
            );
        }

        List<String> categories = new ArrayList<String>();
        List<String> data2 = new ArrayList<String>();

        Collections.reverse(resultList);
        if (HyCfgUtil.getCfg("fahuodingdan")!=null&&HyCfgUtil.isUsed("fahuodingdan")){
            String day1 = HyCfgUtil.getCfgItem2("fahuodingdan","day1");
            data2.add(day1);
            String day2 =HyCfgUtil.getCfgItem2("fahuodingdan","day2");
            data2.add(day2);
            String day3 =HyCfgUtil.getCfgItem2("fahuodingdan","day3");
            data2.add(day3);
            String day4 =HyCfgUtil.getCfgItem2("fahuodingdan","day4");
            data2.add(day4);
            String day5 =HyCfgUtil.getCfgItem2("fahuodingdan","day5");
            data2.add(day5);
            String day6 =HyCfgUtil.getCfgItem2("fahuodingdan","day6");
            data2.add(day6);
            String day7 =HyCfgUtil.getCfgItem2("fahuodingdan","day7");
            data2.add(day7);
            for (String s : resultList) {
                    String[] split = s.split("-");
                    categories.add(split[1] + "-" + split[2]);
            }

        }else {
            for (String s : resultList) {
                HyDaPingVO getzcjje = daPingMapper.getFaHuoDingDan(s);
                if (getzcjje != null) {
                    String[] split = s.split("-");
                    categories.add(split[1] + "-" + split[2]);
                    data2.add(getzcjje.getValue() == null ? "0" : getzcjje.getValue());
                }
            }
        }

        JSONArray series = new JSONArray();
        JSONObject serie = new JSONObject();
        serie.put("data", data2);
        serie.put("name", "单");
        series.add(serie);
        JSONObject data = new JSONObject();
        data.put("categories", categories);
        data.put("series", series);
        JSONObject obj = new JSONObject();
        obj.put("status", 0);
        obj.put("msg", "");
        obj.put("data", data);
        return obj.toJSONString();

    }


    //实时签收滚动屏
    @PostMapping(value = "/ssqsgdp")
    public String ssqsgdp() {
        JSONObject obj = new JSONObject();
        JSONObject data = new JSONObject();

        JSONArray columns = new JSONArray();


        JSONObject column2 = new JSONObject();
        column2.put("name", "姓名");
        column2.put("id", "name");
        columns.add(column2);

        JSONObject column3 = new JSONObject();
        column3.put("name", "车牌号");
        column3.put("id", "che");
        columns.add(column3);

        JSONObject column4 = new JSONObject();
        column4.put("name", "运单号");
        column4.put("id", "num");
        columns.add(column4);

        JSONObject column5 = new JSONObject();
        column5.put("name", "当前状态");
        column5.put("id", "state");
        columns.add(column5);


        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .orderByDesc("create_time").last("limit 16")
        );
        JSONArray rows = new JSONArray();

        for (int i = 0; i < listJd.size(); i++) {
            JSONObject row = new JSONObject();
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            if (jsonObject.get("driverId_dictText") != null) {
                row.put("name", jsonObject.get("driverId_dictText").toString());
            }
            row.put("che", jsonObject.get("vehicleId_dictText") == null ? "" : jsonObject.get("vehicleId_dictText").toString());
            row.put("num", listJd.get(i).getShippingNoteNumber());
            row.put("state", jsonObject.get("shippingStatus_dictText").toString());
            rows.add(row);
        }
        data.put("columns", columns);
        data.put("rows", rows);

        obj.put("status", 0);
        obj.put("msg", "");
        obj.put("data", data);
        return obj.toJSONString();
    }


    //常跑路线
    @PostMapping(value = "/changpaoluxian")
    public String changpaoluxian() {
        JSONArray columns = new JSONArray();
        JSONObject obj = new JSONObject();
        JSONObject data = new JSONObject();

        JSONObject column2 = new JSONObject();
        column2.put("name", "起始地");
        column2.put("id", "subsys");
        columns.add(column2);

        JSONObject column3 = new JSONObject();
        column3.put("name", "目的地");
        column3.put("id", "module");
        columns.add(column3);


        JSONArray rows = new JSONArray();

        //TODO 修改空间换时间
        List<HyDaPingVO> getshippings = daPingMapper.getshippings2();
        for (HyDaPingVO getshipping : getshippings) {
            JSONObject row = new JSONObject();
            HyArea sendAreaCode = getAreaNames(getshipping.getSend() + "000000");
            HyArea receiveAreaCode = getAreaNames(getshipping.getReceive() + "000000");

            if (sendAreaCode != null && receiveAreaCode != null) {
                List<String> sendNames = Arrays.asList(sendAreaCode.getMergerName().split(","));
                List<String> receiveNames = Arrays.asList(receiveAreaCode.getMergerName().split(","));
                if (sendNames.size() > 2 && receiveNames.size() > 2) {
                    row.put("subsys", sendNames.get(2));
                    row.put("module", receiveNames.get(2));
                    rows.add(row);
                } else if (sendNames.size() == 2 && receiveNames.size() == 2) {
                    row.put("subsys", sendNames.get(1));
                    row.put("module", receiveNames.get(1));
                    rows.add(row);
                } else {
                    row.put("subsys", sendNames.get(0));
                    row.put("module", receiveNames.get(0));
                    rows.add(row);
                }
            }

        }

        data.put("columns", columns);
        data.put("rows", rows);

        obj.put("status", 0);
        obj.put("msg", "");
        obj.put("data", data);
        return obj.toJSONString();

    }


    //发货排名2
    @PostMapping(value = "/getPaiming2")
    public String getPaiming2() {
        List<HyDaPingVO> getkehupaiming = daPingMapper.getkehupaimingTop5();

        JSONArray columns = new JSONArray();
        JSONObject obj = new JSONObject();
        JSONObject data = new JSONObject();

        JSONObject column2 = new JSONObject();
        column2.put("name", "客户名称");
        column2.put("id", "name");
        column2.put("width", "70");
        columns.add(column2);


        JSONObject column3 = new JSONObject();
        column3.put("name", "运单数");
        column3.put("id", "module");
        columns.add(column3);


        JSONArray rows = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getkehupaiming) {
            JSONObject row = new JSONObject();
            row.put("name", hyDaPingVO.getName());
            row.put("module", hyDaPingVO.getValue());
            rows.add(row);
        }


        data.put("columns", columns);
        data.put("rows", rows);

        obj.put("status", 0);
        obj.put("msg", "");
        obj.put("data", data);
        return obj.toJSONString();
    }

    private HyArea getAreaNames(String areaCode) {
        LambdaQueryWrapper<HyArea> queryWrapperArea = new LambdaQueryWrapper<>();
        queryWrapperArea.eq(HyArea::getAreaCode, areaCode);
        return hyAreaService.getOne(queryWrapperArea);
    }


}
